import javax.swing.plaf.nimbus.AbstractRegionPainter;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class BankAccountManager {

    private static List<BankAccount> bankAccountList = new ArrayList<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        loadData(); // 加载数据
        while (true) {
            System.out.println("1:添加账户，2:查询账户、3:删除账户、4:存款、5:取款、7:转账、8:退出");
            int i = scanner.nextInt();
            if (i == 1) {
                addAccount(scanner); // 添加
            } else if (i == 2) {
                getAccountList(scanner); // 查询
            } else if (i == 3) {
                removeAccount(scanner); // 删除
            } else if (i == 4) {
                inAccount(scanner); // 存款
            } else if (i == 5) {
                outAccount(scanner); // 取款
            } else if (i == 6) {
                System.out.println("退出");
            } else if (i == 7) {
                trsfaner(scanner);
            } else if (i == 8) {
                saveData();
                System.out.println("bye bye。。。");
                System.exit(-1);
            }
        }
    }

    private static void trsfaner(Scanner scanner) {
        // 1、输入转入账号
        System.out.println("请输入转入账户:");
        String inAccount = scanner.next();

        // 2、输入转出账号
        System.out.println("请输入转出账户:");
        String outAccount = scanner.next();

        // 3.输入金额
        System.out.println("输入转账金额:");
        double money = scanner.nextDouble();

        // 4.检查转入和转出账号是否存在
        BankAccount inBankAccount = getAccount(inAccount);
        if (inBankAccount == null) {
            System.out.println("【" + inAccount + "】转入账号不存在 ");
            return;
        }
        BankAccount outBankAccount = getAccount(outAccount);
        if (outBankAccount == null) {
            System.out.println("【" + outAccount + "】转出账户不存在");
            return;
        }

        // 5.检查金额是否合理，不能出现负数
        if (money <= 0) {
            System.out.println("请输入合理的金额");
            return;
        }

        // 6.检查金额是否超过转出账号的余额
        if (money > outBankAccount.getBalance()) {
            System.out.println("账户余额不足");
            return;
        }

        // 7.给转入账号加钱
        outBankAccount.out(money);

        // 8.给转出账号扣钱
        inBankAccount.in(money);

        System.out.println("转账成功");
    }

    public static void loadData() {
        ObjectInputStream ips = null;
        try {
            ips = new ObjectInputStream(new FileInputStream("./bankAccount.db"));
            bankAccountList = (List<BankAccount>) ips.readObject();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (ips != null) {
                try {
                    ips.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    public static void saveData() {
        ObjectOutputStream ops = null;
        try {
            ops = new ObjectOutputStream(new FileOutputStream("./bankAccount.db"));
            ops.writeObject(bankAccountList);
            System.out.println("落盘成功。。。");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ops != null) {
                try {
                    ops.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }


    private static void outAccount(Scanner scanner) {

        System.out.println("请输入要取款的账户:");
        String outAccount = scanner.next();

        System.out.println("请输入取款的金额:");
        double mooney = scanner.nextDouble();

        // 判断取款账号是否存在
        BankAccount outBankAccount = getAccount(outAccount);
        if (outBankAccount == null) {
            System.out.println("【" + outAccount + "】账户不存在");
            return;
        }

        if (mooney <= -0) {
            System.out.println("请输入正确的金额");
            return;
        }

        // 判断账号的余额是否充足，取的钱小于等于账户的余额
        if (mooney > outBankAccount.getBalance()) {
            System.out.println("账户余额不足");
            return;
        }

        // 扣钱
        outBankAccount.out(mooney);
        System.out.println("取款成功！！！");
    }

    private static void inAccount(Scanner scanner) {
        System.out.println("请输入要存入的账号:");
        String inAccount = scanner.next();

        // 鲁棒性
        System.out.println("请输入要存入的金额:");
        double money = scanner.nextDouble();

        // 判断存入账号是否存在
        BankAccount inBankAccount = getAccount(inAccount);

        if (inBankAccount == null) {
            System.out.println("【" + inAccount + "】账号不存在");
            return;
        }

        // 判断转入的金额是否合理
        if (money <= 0) {
            System.out.println("请输入正确的金额");
            return;
        }

        // 实现存款
        inBankAccount.in(money);
        System.out.println("存款成功");
    }

    // 根据账号找账号对象
    private static BankAccount getAccount(String account) {
        for (BankAccount bankAccount : bankAccountList) {
            if (bankAccount.getAccount().equals(account)) {
                return bankAccount;
            }
        }
        return null;
    }

    private static void removeAccount(Scanner scanner) {
        System.out.println("请输入要删除的账户");
        String account = scanner.next();

        boolean isDelete = false; // 用这个标志记录是否删除成功

        // 1.从集合中找到这个账户
        for (BankAccount bankAccount : bankAccountList) {
            if (bankAccount.getAccount().equals(account)) {
                bankAccountList.remove(bankAccount);
                isDelete = true;
                System.out.println("删除成功！！！");
                break;
            }
        }

        if (!isDelete) {
            System.out.println("【" + account + "】不存在");
        }
    }

    private static void getAccountList(Scanner scanner) {
        for (BankAccount bankAccount : bankAccountList) {
            System.out.println(bankAccount);
        }
    }

    private static void addAccount(Scanner scanner) {
        System.out.println("请输入添加的账户:");
        String account = scanner.next();

        System.out.println("请输入账号金额:");
        double balance = scanner.nextDouble();

        // 封装一下
        BankAccount bankAccount = new BankAccount(account, balance);

        // 添加到集合中
        bankAccountList.add(bankAccount);

        System.out.println("添加成功");
    }
}
